﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace _51Yii.DomainService
{
    public class ProxyCrawler
    {

        char[] buffer = new char[1024];

        public void StartCrawl(string uri)
        {
            HttpAsyncState asyncState = new HttpAsyncState();

            HttpWebRequest request = WebRequest.CreateHttp(uri);

            asyncState.Request = request;

            request.BeginGetResponse(new AsyncCallback(ResponseCallback), asyncState);


        }

        public void ResponseCallback(IAsyncResult obj)
        {
            HttpAsyncState asyncState = obj.AsyncState as HttpAsyncState;

            if (asyncState != null)
            {
                HttpWebRequest request = asyncState.Request;

                HttpWebResponse response = request.EndGetResponse(obj) as HttpWebResponse;

                if (response != null)
                {

                    using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream, UnicodeEncoding.GetEncoding("gb2312")))
                    {
                        

                        string respString = reader.ReadToEnd();


                    }

                }

            }
        }

    }

    public class HttpAsyncState
    {
        public HttpWebRequest Request { get; set; }

    }
}
